Import brl.httprequest
Import brl.databuffer
Import mojo.asyncloaders

Class CalLoader 'Base Class for Loading
	
	Field loadedobject:Object
	Field loaded:Bool = False
	
	Method Load:Object(data:String)
	
		loadedobject =  New Object
		loaded = True
		Return loadedobject
	End
	
	Method IsLoaded:Bool()
		Return loaded
	End
	
	Method GetLoadedObject:Object()
		Return loadedobject
	End

End


Class CalLoaderHandler

	Const DATACALSKELETON:Int = 	0
	Const DATACALMESH:Int = 		1
	Const DATACALANIMATION:Int = 	2
	Const DATACALMORPHANIM:Int = 	3
	Const DATACALMATERIAL:Int = 	4
	Const DATACALIMAGE:Int =	 	5
	
	
	Field LoaderCallbacks:List<CalLoadingCallback>
	
	
	Method New()
		LoaderCallbacks = New List<CalLoadingCallback>	
	End
	
	
	Method LoadFile(url:String,type:Int,loader:CalLoader,data:String="")
		
		'no need to reload our data if it's cached.
		Local havecallback:Bool = False
		
		For Local cb:CalLoadingCallback = Eachin LoaderCallbacks
		
			If cb.fileurl = url Then
				'Print "Have callback"
				havecallback = True
				'Exit
			Endif
			
		Next
		If havecallback = false then
			Local callback:CalLoadingCallback = New CalLoadingCallback
			callback.SetLoader(loader)
			callback.Load(type,url,data)
			LoaderCallbacks.AddLast(callback)
		Endif
		
		
	End

	Method LoadData(url:String,type:Int,loader:CalLoader,data:String="")
		Local callback:CalLoadDataBufferCallback = New CalLoadDataBufferCallback
		callback.SetLoader(loader)
		callback.Load(type,url,data)
		LoaderCallbacks.AddLast(callback)
	End

	Method LoadImage(url:String,type:Int,loader:CalLoader,data:String="")
		Local callback:CalLoadImageCallback = New CalLoadImageCallback
		callback.SetLoader(loader)
		callback.Load(type,url,data)
		LoaderCallbacks.AddLast(callback)
	End
	
	Method Update()
		For Local callback:CalLoadingCallback = Eachin LoaderCallbacks
			callback.Update()
			If callback.IsFinished = True And callback.loader.IsLoaded() = False Then
				callback.loader.Load(callback.data)
			Endif
		Next
	End
	
	Method IsFileLoaded:Bool(url:String)
		For Local callback:CalLoadingCallback = Eachin LoaderCallbacks
			If callback.fileurl = url And callback.IsFinished = True And callback.loader.IsLoaded() = True Then
				Return True
			Endif
		Next	
		Return False
	End
	
	Method GetLoadedFile:Object(url:String,keepinlist:Bool=True)
		For Local callback:CalLoadingCallback = Eachin LoaderCallbacks
			If callback.fileurl = url And callback.IsFinished = True And callback.loader.IsLoaded() = True Then
				If keepinlist = False Then
					LoaderCallbacks.Remove(callback)
				Endif
				Return callback.GetLoadedObject()
			Endif
		Next	
		Return Null	
	End
	
	
End Class


Class CalLoadingCallback Implements IOnHttpRequestComplete

	Field get_req:HttpRequest
	Field post_req:HttpRequest
	Field hasheader:Bool = false
	Field headername:String
	Field headerval:String
	Field bytesrec:Int
	Field data:String
	Field finished:Bool = False
	Field datatype:Int
	Field fileurl:String
	Field loader:CalLoader
	
	Method GetLoadedObject:Object()
		Return loader.GetLoadedObject()
	End
	
	Method SetLoader:Void(loadr:CalLoader)
		loader = loadr
	End
	
	Method GetLoader:CalLoader()
		Return loader
	End
	
	Method SetDataType:Void(datat:Int)
		datatype = datat
	End
	
	Method GetDataType:Int()
		Return datatype
	End
	
	Method IsFinished:Bool()
		Return finished
	End
	
	Method GetData:String()
		Return data
	End
	
	Method OnHttpRequestComplete:Void( req:HttpRequest )
		'UpdateAsyncEvents
		
		Print "Status: "+req.Status()
		data = req.ResponseText()
		finished = True
		
		
	End
	
	Method SetHeader:Void(name:String, value:String)
		headername = name
		headerval = val
		hasheader = True
	End
	
	Method RemoveHeader:Void()
		headername = ""
		headerval = ""
		hasheader = False		
	End
	
	Method Update:Void()
		
		
		If get_req <> Null Then
			bytesrec = get_req.BytesReceived()
		Endif
		If post_req <> Null Then
			bytesrec = post_req.BytesReceived()
		Endif	
		
		
			
	End
	
	
	Method Load:Void(type:Int=0,url:String,data:String="",mime:String="text/plain;charset=utf-8",enc:String="utf8")
		fileurl = url
		Print "Loading: "+url
		Select type
		
		
			Case 0
		        get_req=New HttpRequest( "GET",url,Self )
	        	If hasheader = True Then
	        		get_req.SetHeader(headername,headerval)
	        	Endif		        
		        If data <> "" then
		        	get_req.Send(data,mime,enc)
		        Else
		        	get_req.Send()
		        Endif
		        				
			Case 1
		        post_req=New HttpRequest( "POST",url,Self )
	        	If hasheader = True Then
	        		post_req.SetHeader(headername,headerval)
	        	Endif		        
		        If data <> "" then
		        	post_req.Send(data,mime,enc)
		        Else
		        	post_req.Send()
		        Endif		
		End Select
		
		
	End
	
	
	

End Class


Class CalLoadImageCallback Extends CalLoadingCallback Implements IOnLoadImageComplete

	Field newimage:Object
	
	
	Method GetLoadedObject:Object()
		Return newimage
	End

	Method OnLoadImageComplete:Void( image:Image, path:String, source:IAsyncEventSource )
		finished = True
		Print "Loaded Image: "+path
		newimage = Object(image)
	End
	
	
	Method Load:Void(type:Int=0,url:String,data:String="",mime:String="text/plain;charset=utf-8",enc:String="utf8")
		fileurl = url
		Print "Loading: "+url
		
		LoadImageAsync( url,1,Image.DefaultFlags,self )
		
		
	End	
	

End Class





